package com.likerSai;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 罗马数字转整数
 *
 * @author sun.ye
 * @since 2021-06-07 09:46
 */
public class RomanToInt {



    public static int romanToInt(String s) {
        char[] chars = s.toCharArray();
        int sum = 0;
        for (int i = 0 ;i < chars.length; i++) {
            char c = chars[i];
            if (c == 'I'){
                if (indexIs(chars,i+1,'V')){
                    sum += 4;
                    i++;
                }else if(indexIs(chars,i+1,'X')) {
                    sum += 9;
                    i++;
                }else {
                    sum += 1;
                }
            }
            if (c == 'X'){
                if (indexIs(chars,i+1,'L')){
                    sum += 40;
                    i++;
                }else if(indexIs(chars,i+1,'C')) {
                    sum += 90;
                    i++;
                }else {
                    sum += 10;
                }
            }
            if (c == 'C'){
                if (indexIs(chars,i+1,'D')){
                    sum += 400;
                    i++;
                }else if(indexIs(chars,i+1,'M')) {
                    sum += 900;
                    i++;
                }else {
                    sum += 100;
                }
            }
            if (c == 'V'){
                sum += 5;
            }
            if (c == 'L'){
                sum += 50;
            }
            if (c == 'D'){
                sum += 500;
            }
            if (c == 'M'){
                sum += 1000;
            }
        }
        return sum;
    }


    public static boolean indexIs(char[] chars,int index,char c){
        if (index >= chars.length){
            return false;
        }else {
            return chars[index] == c;
        }
    }
}